package com.appcenter.application.mapper;

import com.appcenter.application.dto.RoleUserDto;
import com.appcenter.application.input.RoleUser.GetRoleUsersInput;
import com.appcenter.application.input.RoleUser.GetRoleUsersServiceInput;
import com.appcenter.application.model.Apps;
import com.appcenter.application.model.RoleUsers;
import com.appcenter.application.output.RoleUser.GetRoleUsersOutput;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;

@org.apache.ibatis.annotations.Mapper
@Repository
public interface RoleUsersMapper extends Mapper<RoleUsers> {


    @Delete("delete from roleusers where UserId=#{UserId}")
    public int deleteRoleUserByUserId(String UserId);



    @Select("<script>" +
            "select a.*,d.AppName,b.RoleName,c.UserCode,c.UserName from roleusers a " +
            " inner JOIN roles b on a.RoleId=b.Id" +
            " inner join users c on c.Id=a.UserId" +
            " inner JOIN apps d on b.AppId=d.Id" +
            " where 1=1 " +
            "" +
            " <when test='keyword!=null'>" +
            " AND (c.UserCode like concat('%',#{keyword},'%') or c.UserName like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            " <when test='AppId!=null'>" +
            " AND b.AppId=#{AppId}" +
            " </when>" +
            "" +
            " <when test='RoleId!=null'>" +
            " AND a.RoleId=#{RoleId}" +
            " </when>" +
            "" +
            "" +
            "</script>")
    List<RoleUserDto> searchRoleUser(GetRoleUsersInput input);

    @Select("<script>" +
            "SELECT RoleUsers.Id,Apps.AppName,Users.Id as UserId,Users.UserCode,Users.UserName,Roles.Id as RoleId,Roles.RoleName from RoleUsers"+
            " INNER JOIN Users  ON Users.Id=RoleUsers.UserId"+
            " INNER JOIN Roles ON Roles.Id=RoleUsers.RoleId "+
            " INNER JOIN Apps ON Apps.Id=Roles.AppId"+
            " where 1=1"+
            "" +
            " <when test='keyword!=null'>" +
            " AND ( Users.UserCode like concat('%',#{keyword},'%') or Users.UserName like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            " <when test='AppId!=null'>" +
            " AND Roles.AppId=#{AppId}"+
            " </when>" +
            "" +
            " <when test='RoleId!=null'>" +
            " AND Roles.Id=#{RoleId}"+
            " </when>" +
            "" +
            "  ORDER BY UserCode"+
            "</script>")
    List<RoleUserDto> getRoleUsers(GetRoleUsersServiceInput input);








}
